package TangramBase;

import TangramCore.Dilek;
import TangramCore.Hladina;
import TangramCore.HladinaObrys;
import TangramCore.Obrazec;
import TangramCore.Obrys;
import TangramCore.Skladacka;
import TangramCore.Vzhled;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.util.ArrayList;

public class ObrazecLayout extends Obrazec implements DropTargetListener {
	static final long serialVersionUID = 24362462L;
	
    Rectangle vel;//Dimension dim;
    double ratio; 
    private static final int OKRAJ = 10;
    DragSource dragSource = null;
    DropTarget dropTarget = null;
    HladinaObrys hladinaDragPreview;
    Rectangle rectDrag = new Rectangle(0,0,-50,-50);
    
    public void nastavVelikost(int w, int h){
        int width, height, x, y;

        Rectangle tmp = new Rectangle(0,0,0,0);
        getInsideRect(tmp);
        if((double)w / h > ratio){
            height = h - 2*OKRAJ;
            y = OKRAJ;
            width = (int)(ratio * (height - tmp.height)) + tmp.width;
            x = (int)((w - width) / 2) + tmp.x;
        }
        else {
            width = w - 2*OKRAJ;
            x = OKRAJ;
            height = (int)((width - tmp.width) / ratio) + tmp.height;
            y = (int)((h - height) / 2) + tmp.y;
        }
        setBounds(x, y, width, height);    
    }
            
    public ObrazecLayout (Skladacka skl, Paint pb, double r, int width, int height){
        super(skl, pb, new ArrayList<Hladina>(), Obrazec.MOD_LAYOUT);
        Dilek[] d = skl.getDilky();
        Obrys o = new Obrys(d);
        hladinaDragPreview = new HladinaObrys(d,o,Vzhled.getDefaultNahledDilek(),Vzhled.getDefaultNahledFilled(),null,Hladina.CENTER,false, new Rectangle(0,0,-50,-50));
        addHladina(hladinaDragPreview);
        hladinaDragPreview.hide();
        ratio = r;
        nastavVelikost(width, height);
        dropTarget = new DropTarget(this, this);
        dragSource = new DragSource();
    }
    
    public void dragEnter(DropTargetDragEvent dtde) {
            try{
                ObrazecShow.ObrazecTransferable data = (ObrazecShow.ObrazecTransferable)(dtde.getTransferable().getTransferData(ObrazecShow.ObrazecTransferable.flavorObrazec));
                dtde.acceptDrag(DnDConstants.ACTION_COPY);
                Dilek[] dn = skladacka.getDilky(data.d);
                Obrys on = new Obrys(dn, data.od);
                Point p = dtde.getLocation();
                int rw = -50, rh = -50, w = getWidth(), h = getHeight();
                int rx = (int)(-100.0f * p.x / w - 0.5f * rw);
                int ry = (int)(-100.0f * p.y / h - 0.5f * rh);
                hladinaDragPreview.update(dn, on);
                hladinaDragPreview.pos.setBounds(rx, ry, rw, rh);
                hladinaDragPreview.resize();
                hladinaDragPreview.invalidate();
                hladinaDragPreview.toggleVisibility();
                //HladinaObrys hn = new HladinaObrys(dn, on, Vzhled.getDefaultNahledDilek(), Vzhled.getDefaultNahledFilled(), null, Hladina.CENTER, false, new Rectangle(rx, ry, rw, rh));
                //addHladina(hn);
                update();
            }catch(Exception ex){}

    }
    public void dragExit(DropTargetEvent dte) {
        hladinaDragPreview.hide();
        update();
    }
    public void dragOver(DropTargetDragEvent dtde) {
        Point p = dtde.getLocation();
        int rw = -50, rh = -50, w = getWidth(), h = getHeight();
        int rx = (int)(-100.0f * p.x / w - 0.5f * rw);
        int ry = (int)(-100.0f * p.y / h - 0.5f * rh);
        hladinaDragPreview.pos.setBounds(rx, ry, rw, rh);
        hladinaDragPreview.resize();
        hladinaDragPreview.invalidate();
        update();
        
    }
    public void dropActionChanged(DropTargetDragEvent dtde) {    }
    
    public void drop(DropTargetDropEvent dtde) {
        //if(dtde.isDataFlavorSupported(ObrazecShow.ObrazecTransferable.flavorObrazec)){
            try{
                ObrazecShow.ObrazecTransferable data = (ObrazecShow.ObrazecTransferable)(dtde.getTransferable().getTransferData(ObrazecShow.ObrazecTransferable.flavorObrazec));
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
                Dilek[] dn = skladacka.getDilky(data.d);
                Obrys on = new Obrys(dn, data.od);
                Point p = dtde.getLocation();
                int rw = -50, rh = -50, w = getWidth(), h = getHeight();
                int rx = (int)(-100.0f * p.x / w - 0.5f * rw);
                int ry = (int)(-100.0f * p.y / h - 0.5f * rh);
                HladinaObrys hn = new HladinaObrys(dn, on, Vzhled.getDefaultNahledDilek(), Vzhled.getDefaultNahledFilled(), null, Hladina.CENTER, false, new Rectangle(rx, ry, rw, rh));
                addHladina(hn);
                hladinaDragPreview.toggleVisibility();
                update();
            }catch(Exception ex){}
        //}
    }



}
